package com.junxian.demo.common;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author sunjx
 * @program heredata
 * @description 导入导出excel时，自定义属性格式转换类型
 * @date 2020-09-25 15:22
 */
public class AlarmTypeConverter implements Converter<String> {

    private static final Logger logger = LoggerFactory.getLogger(AlarmTypeConverter.class);

    /**
     * 严重告警
     */
    private static String SERIOUSALARM = "严重告警";

    /**
     * 重要告警
     */
    private static String IMPORTALARM = "重要告警";

    /**
     * 一般告警
     */
    private static String COMMONALARM = "一般告警";

    @Override
    public Class supportJavaTypeKey() {
        return Integer.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

    /**
     * @description 这里读的时候会调用(从excel中导入的时候把相关属性进行转换)
     * @author sunjx
     * @date 2020/9/27 14:26
     * @param cellData
     * @param contentProperty
     * @param globalConfiguration
     * @return java.lang.String
     **/
    @Override
    public String convertToJavaData(CellData cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration){
        String readValue = cellData.getStringValue();
        String converterValue = "";
        if (SERIOUSALARM.equals(readValue)) {
            converterValue = "1";
        }else if (IMPORTALARM.equals(readValue)) {
            converterValue = "2";
        }else if (COMMONALARM.equals(readValue)) {
            converterValue = "3";
        }
        logger.info("AlarmTypeConverter，convertToJavaData，alarmType格式转换，readValue：{}, converterValue:{}", readValue, converterValue);
        return converterValue;
    }

    /**
     * @description 这里是excel写的时候会调用,（就是导出写入excel的时候会调用）
     *              把数据库中alarmType为1、2、3转换为严重、重要、一般告警
     * @author sunjx
     * @date 2020/9/27 11:49
     * @param value
     * @param contentProperty
     * @param globalConfiguration
     * @return com.alibaba.excel.metadata.CellData
     **/
    @Override
    public CellData convertToExcelData(String value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration){
        String writeValue = value;
       if ("1".equals(value)) {
            writeValue = SERIOUSALARM;
        }else if ("2".equals(value)) {
            writeValue = IMPORTALARM;
        }else if ("3".equals(value)) {
            writeValue = COMMONALARM;
        }
        logger.info("AlarmTypeConverter，convertToExcelData，alarmType格式转换，value：{}, writeValue:{}", value, writeValue);
        return new CellData(writeValue);
    }
}
